moveStroe.controller( 'fileCtr', function( $scope,$rootScope,$cookieStore, $http, $location,$stateParams, myAlert,alertContent){
  var cid=$stateParams.id;
  $scope.table='';
  $scope.currpath=''; //当前路径



  //获取数据
  $scope.getDir=function($file){
    $http({method:'get',params:{fileName:$file},url:'/File/Home/getDir'}).then(function success(data){
     var result=data.data.msg;

      if(data.data.status==false){
        myAlert.open(data.data.msg);
      }
      if(result.type=='dir'){
          $scope.table=result.data;
          for( v in result.data){
            if(result.data[v].fileName=='.'){
              $scope.currpath=result.data[v].rootName;
            }
          }
      }else if(result.type=='file'){
        result.curPath= $scope.currpath;
        alertContent.file(result,'lg')
      }
    },function error(data){
      myAlert.open('获取数据失败')
    })
  }
  var data=$cookieStore.get("path_url");
  if(data!='undefined'){
    $scope.getDir(data);
  }else{
  $scope.getDir(data);
  }
})
moveStroe.controller( 'addCtr', function( $scope, $http, $state, $location,$stateParams,Upload,tree, myserver, fromdata, myAlert ){
 var cid=$stateParams.id;

  //单图片
  $scope.image={
    url:'',
    id:'',
  };
  // 多文件上传
  $scope.images = [];
  //上传进度
  $scope.progessload = 0;
  //接受值
  //监听服务上传进度
  $scope.$on( 'progessload', function( e, data ){
    $scope.progessload = data;
  } );
  $scope.$on( 'images', function( e, data ){
    if(data.msg.type==1){
      $scope.image.url = data.msg.img;
      $scope.image.id = data.msg.id;
    }else if(data.msg.type==2){
      $scope.images.push( {url:data.msg.img,id:data.msg.id} )
    }else{
      myAlert.open( data.msg )
    }
  } );

  $scope.submits = function(type){
    myserver.imgUpload( $scope.file ,type);
  };


  $scope.froms = {
    'title': '',
    'intro': '',
    'logo': '',
    'imgs': '',
    'category_id': cid,
    'moveintro': '',
    'resource': '',
    'status': true,
    'tab': '',
  }
  $scope.category=[];


  //
  // $scope.ready = function(editor){
  //     editor.setContent( 'asd');
  //
  // }
  // $scope.config = {
  //   toolbars: [
  //     ['FullScreen', 'Source', 'Undo', 'Redo', 'Bold', 'test']
  //   ],
  // }

  var getCategory=function(){
    $.get( '/Admin/Category/gets',function( data ){
      var ret= data.msg.data
      var ret=tree.conv(data.msg)
      for(b in ret){
        ret[b].tree1='';
        for( var i=1;i<ret[b].tree;i++){
          ret[b].tree1+='++';
        }
      }
      $scope.category=ret;
      $scope.$apply();//需要手动刷新
    } )
  }
  getCategory();


  $scope.formsButton = function(){

    var imgs='';
    if($scope.images.length>0){
          for(i in $scope.images){
            imgs+=$scope.images[i].id+'|'

          }
    }

    var data = $scope.froms;
    data.imgs=imgs;
    data.logo=$scope.image.id;
    data.status=data.status?1:0

    if( !fromdata.require( data.title ) ){
      myAlert.open( "标题不能为空" )
      return;
    }
    if( !fromdata.require( data.moveintro ) ){
      myAlert.open( "简介不能为空" )
      return;
    }
    if( !fromdata.require( data.resource ) ){
      myAlert.open( "资源不能为空" )
      return;
    }
    $.post( '/Admin/Move/save', $scope.froms, function( data ){
      if( data.status ){
        myAlert.open( data.msg );
        $state.go( 'index.move' );
      }else{
        myAlert.open( data.msg );
      }
    } )
  }
} );
moveStroe.controller( 'editCtr', function( $scope, $http, $state, $stateParams,$location,Upload,tree,myserver, fromdata, myAlert ){
  var cid=$stateParams.id; //分类id
  var aid=$stateParams.aid; //文章id
  if(aid==''||aid=='undefined'){
    $state.go( 'index.move');
  }
  //获取编辑数据
  $scope.froms = {
     'id':'',
    'title': '',
    'intro': '',
    'logo': '',
    'imgs': '',
    'category_id':'',
    'moveintro': '',
    'resource': '',
    'status': true,
    'tab': '',
  }

  $scope.category=[];
  //单图片
  $scope.image={
    url:'',
    id:'',
  };
  // 多文件上传
  $scope.images = [

  ];
  // $scope.ready = function(editor){
  //   editor.setContent($scope.froms.moveintro, true);
  // }
  //上传进度
  $scope.progessload = 0;
  //接受值
  //监听服务上传进度
  $scope.$on( 'progessload', function( e, data ){
    $scope.progessload = data;
  } );
  $scope.$on( 'images', function( e, data ){
    if(data.msg.type==1){
      $scope.image.url = data.msg.img;
      $scope.image.id = data.msg.id;
    }else if(data.msg.type==2){
      $scope.images.push( {url:data.msg.img,id:data.msg.id} )
    }else{
      myAlert.open( data.msg )
    }
  } );



  $http({
    method: "get",
    params:{id:aid},
    url:"/Admin/Move/get"
  }).success(function(data){
    if(data.status){
      $scope.froms=data.msg;
      $scope.image.url=$scope.froms.logo.url;
      $scope.image.id=$scope.froms.logo.id;
      if($scope.froms.imgs!=''){
        $scope.images=$scope.froms.imgs;
      }
    }else{
      $state.go( 'index.move');
    }
  }).error(function( data ){
  });





  $scope.submits = function(type){
    myserver.imgUpload( $scope.file ,type);
  };

  var getCategory=function(){
    $.get( '/Admin/Category/gets',function( data ){
      var ret= data.msg.data
      var ret=tree.conv(data.msg)
      for(b in ret){
        ret[b].tree1='';
        for( var i=1;i<ret[b].tree;i++){
          ret[b].tree1+='++';
        }
      }
      $scope.category=ret;
      $scope.$apply();//需要手动刷新
    } )
  }
  getCategory();


  $scope.formsButton = function(){

    var imgs='';
    if($scope.images.length>0){
          for(i in $scope.images){
            imgs+=$scope.images[i].id+'|'

          }
    }
    var data = $scope.froms;
    data.imgs=imgs;
    data.logo=$scope.image.id;
    data.status=data.status?1:0
    if( !fromdata.require( data.title ) ){
      myAlert.open( "标题不能为空" )
      return;
    }
    if( !fromdata.require( data.moveintro ) ){
      myAlert.open( "简介不能为空" )
      return;
    }
    if( !fromdata.require( data.resource ) ){
      myAlert.open( "资源不能为空" )
      return;
    }
    $.post( '/Admin/Move/update', $scope.froms, function( data ){
      if( data.status ){
        myAlert.open( data.msg );
        $state.go( 'index.move',{id:cid});
      }else{
        myAlert.open( data.msg );
      }
    } )
  }
} );
